package com.hualian.coupon.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualian.coupon.service.MemCouponService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lxd
 * @Description TODO
 * @date 2020/10/23 18:58
 * @Version 1.0
 */
@RestController
@RequestMapping("/memCop")
public class MemCouponController {

    @Resource
    MemCouponService memCouponService;

    @PostMapping("/list")
    public Map<String, Object>getList(@RequestParam("phone") @Valid String phone,
                                             @RequestParam("page") Integer page) {
        PageHelper.startPage(page, 10);
        List<Map<String, Object>> maps = memCouponService.queryAll(phone);
        return returnPage(maps);
    }

    @GetMapping("useList")
    public Map<String, Object> getUseList(String startDate,String endDate,Integer page) {
        PageHelper.startPage(page, 10);
        List<Map<String, Object>> maps = memCouponService.queryUse(startDate, endDate);
        return returnPage(maps);
    }

    private Map<String, Object> returnPage(List<Map<String, Object>> maps) {
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(maps);
        Map<String, Object> map = new HashMap<>(2);
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }
}
